﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace huaguwang.hgmanage.Bogu
{
    public partial class BoGuGuPiao_E : System.Web.UI.Page
    {
        D_T_StockCode _dtsc = new D_T_StockCode();
        D_T_StockCode_Exp _dtsc_exp = new D_T_StockCode_Exp();
        #region 固定方法
        /// <summary>
        ///  设置按钮资料
        /// </summary>
        /// <param name="i"></param>
        /// <param name="but"></param>
        private void SetBut(int i, Button but)
        {
            AdminPublic.Try(() =>
            {
                switch (i)
                {
                    case 1:
                        but.CausesValidation = false;
                        break;
                    default:
                        break;
                }
            });

        }
    
        /// <summary>
        /// 操作完成后,处理各种提示用
        /// </summary>
        /// <param name="iRef"></param>
        /// <param name="LogType"></param>
        public void EditSaveMsg(int iRef, Log_Type LogType)
        {
            if (iRef > 0)
            {
                this.Exp_PageWriteLog(LogType, " 股票资料维护", iRef);
                this.Exp_PageSkyAlterReplace("BoGuGuPiao.aspx", WebUserToolBar1.EditSKey, "操作成功");
            }
            else
            {
                switch (iRef)
                {
                    case 0:
                        this.Exp_PageAlter("操作失败,操作中出现异常");
                        break;
                    case -1:
                        this.Exp_PageAlter("操作失败,用户资料操作失败");
                        break;
                    case -2:
                        this.Exp_PageAlter("操作失败,权限组资料操作失败");
                        break;
                    default:
                        this.Exp_PageAlter("操作失败");
                        break;
                }
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //工具栏初始化
                WebUserToolBar1.EditInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr);
                if (!IsPostBack)
                {
                
                    //存储主键
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("ID", id);
                    if (id > 0)
                    {
                        var model = _dtsc.GetModel(id);
                        if (model == null)
                        {
                            this.Exp_PageSkyAlterReplace("BoGuGuPiao.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            SetControlsData(model);
                        }
                    }
                }

            });
        }
        private void SetControlsData(M_T_StockCode _mtsc)
        {
            this.txt_code.Text = _mtsc.Code;
            this.txt_title.Text = _mtsc.Title;
            this.txt_Contents.Text = _mtsc.Contents;
        }
        /// <summary>
        /// 从页面控件中构造一个实体model
        /// </summary>
        /// <returns></returns>
        private M_T_StockCode ControlsToModel()
        {
            var model = new M_T_StockCode()
            {
                Flag = true,
                id = ViewState["ID"].Exp_IntTryParse(),
                Code = this.txt_code.Text.Exp_Trim(),
                Title = this.txt_title.Text.Exp_Trim(),
                Contents = this.txt_Contents.Text.Exp_Trim()
            };
            return model;
        }
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
           
            AdminPublic.Try(() =>
           {
               //注意,统一,先获取实体,然后根据实体进行各种判断
               var model = ControlsToModel();

               var LogType = Log_Type.add;
            
               var iRef = 0;
               if (0 >= model.id)
               {
                   iRef = _dtsc.Add(model);
               }
               else
               {
                   //    修改的专用判断
                   LogType = Log_Type.update;
                   var Temp = _dtsc.Exists(model.id);
                   if (!Temp)
                   {
                       this.Exp_PageAlter("资料不存在,可能被其他人删除");
                       return;
                   }

                   iRef = _dtsc.Update(model);
               }
               EditSaveMsg(iRef, LogType);
           });
        }

        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.EditSKey;
                Response.Redirect("BoGuGuPiao.aspx?" + sKey);
            });
        }
    }
}
